package com.bookRecommend.provider;

import com.bookRecommend.model.Book;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

public class BookProvider implements ProviderMethodResolver {

    //book对象
    public String selectByBook(Book book) {
        SQL sql = new SQL().SELECT("*").FROM("book");
        String type = "select";
        return getSql(sql, book, type);
    }

    public String insertBook(Book book) {
        SQL sql = new SQL().INSERT_INTO("book");
        String type = "insert";
        return getSql(sql, book, type);
    }


    public String updateBook(Book book){
        SQL sql = new SQL().UPDATE("book");
        String type = "update";
        return getSql(sql,book,type);
    }

    public String deleteBook(Book book){
        SQL sql = new SQL().DELETE_FROM("book");
        String type = "delete";
        return getSql(sql,book,type);
    }

    private String getSql(SQL sql, Book book, String type) {
        if (book != null) {
            if (book.getId() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("id like '${id}'");
                    case "update", "delete" -> sql.WHERE("id = #{id}");
                    case "insert" -> sql.VALUES("id", "#{id}");
                }
            }
            if (book.getBookName() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("bookname like '${bookName}'");
                    case "update" -> sql.SET("bookname = #{bookName}");
                    case "delete" -> sql.WHERE("bookname = #{bookName}");
                    case "insert" -> sql.VALUES("bookname", "#{bookName}");
                }
            }
            if (book.getTypeId() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("typeid like '${typeId}'");
                    case "update" -> sql.SET("typeid = #{typeId}");
                    case "delete" -> sql.WHERE("typeid = #{typeId}");
                    case "insert" -> sql.VALUES("typeid", "#{typeId}");
                }
            }
            if (book.getAuthor() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("author like '${author}'");
                    case "update" -> sql.SET("author = #{author}");
                    case "delete" -> sql.WHERE("author = #{author}");
                    case "insert" -> sql.VALUES("author", "#{author}");
                }
            }
            if (book.getPress() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("press like '${press}'");
                    case "update" -> sql.SET("press = #{press}");
                    case "delete" -> sql.WHERE("press = #{press}");
                    case "insert" -> sql.VALUES("press", "#{press}");
                }
            }
            if (book.getPrice() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("price like '${price}'");
                    case "update" -> sql.SET("price = #{price}");
                    case "delete" -> sql.WHERE("price = #{price}");
                    case "insert" -> sql.VALUES("price", "#{price}");
                }
            }
            if (book.getIsbn() != null) {
                switch (type) {
                    case "select" -> sql.WHERE("isbn like '${isbn}'");
                    case "update" -> sql.SET("isbn = #{isbn}");
                    case "delete" -> sql.WHERE("isbn = #{isbn}");
                    case "insert" -> sql.VALUES("isbn", "#{isbn}");
                }
            }
            if (book.getImage() != null) {
                switch (type) {
                    case "update" -> sql.SET("image = #{image}");
                    case "insert" -> sql.VALUES("image", "#{image}");
                }
            }
            if (book.getContent() != null) {
                switch (type) {
                    case "update" -> sql.SET("content = #{content}");
                    case "delete" -> sql.WHERE("content = #{content}");
                    case "insert" -> sql.VALUES("content", "#{content}");
                }
            }
        }
        return sql.toString();
    }
}
